package com.ruoyi.youtian.service.impl;

import java.util.List;

import com.ruoyi.Gzmx_device.domain.Gzmx_device;
import com.ruoyi.Gzmx_device.mapper.Gzmx_deviceMapper;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.gzmx_tag.domain.Gzmx_tag;
import com.ruoyi.gzmx_tag.mapper.Gzmx_tagMapper;
import com.ruoyi.youtian.domain.PointAttribute;
import com.ruoyi.youtian.domain.PointEquipment;
import com.ruoyi.youtian.mapper.PointAttributeMapper;
import com.ruoyi.youtian.mapper.PointEquipmentMapper;
import com.ruoyi.youtian.service.IPointEquipmentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * 点位与设备关联Service业务层处理
 *
 * @author ruoyi
 * @date 2022-09-15
 */
@Service
public class PointEquipmentServiceImpl implements IPointEquipmentService
{
    @Autowired
    private PointEquipmentMapper pointEquipmentMapper;

    @Autowired
    private PointAttributeMapper pointAttributeMapper;


    @Autowired
    private Gzmx_tagMapper gzmxTagMapper;

    @Autowired
    private Gzmx_deviceMapper deviceMapper;

    /**
     * 查询点位与设备关联
     *
     * @param id 点位与设备关联主键
     * @return 点位与设备关联
     */
    @Override
    public PointEquipment selectPointEquipmentById(Long id)
    {
        return pointEquipmentMapper.selectPointEquipmentById(id);
    }

    /**
     * 查询点位与设备关联列表
     *
     * @param pointEquipment 点位与设备关联
     * @return 点位与设备关联
     */
    @Override
    public List<PointEquipment> selectPointEquipmentList(PointEquipment pointEquipment)
    {
        return pointEquipmentMapper.selectPointEquipmentList(pointEquipment);
    }

    /**
     * 新增点位与设备关联
     *
     * @param pointEquipment 点位与设备关联
     * @return 结果
     */
    @Override
    @Transactional
    public int insertPointEquipment(PointEquipment pointEquipment)
    {
        Gzmx_device device = deviceMapper.selectGzmx_deviceById(pointEquipment.getEquipmentId());
        if(device !=null ){
            // 更新同步设备安装点位至采集点位安装位置
            Gzmx_tag gzmxTag = new Gzmx_tag();
            gzmxTag.setSourceTag(pointEquipment.getPointId());
            gzmxTag.setEjdw_name(pointEquipment.getEjdwName());
            gzmxTag.setEjdw_id(device.getDev_azdw2_id());
            gzmxTag.setEjdw_name(device.getDev_azdw2_name());
            gzmxTag.setDw_3_id(device.getDev_azdw3_id());
            gzmxTag.setDw_3_name(device.getDev_azdw3_name());
            gzmxTag.setDw_4_id(device.getDev_azdw4_id());
            gzmxTag.setDw_4_name(device.getDev_azdw4_name());
            gzmxTag.setDw_5_id(device.getDev_azdw5_id());
            gzmxTag.setDw_5_name(device.getDev_azdw5_name());
            gzmxTagMapper.updateGzmx_tagSourceTag(gzmxTag);
        }
        PointAttribute pointAttribute = new PointAttribute();
        pointAttribute.setPointId(pointEquipment.getPointId());
        pointAttribute.setSceneId(-1L);
        pointAttribute.setUpperLimit(pointEquipment.getMaximum());
        pointAttribute.setLowerLimit(pointEquipment.getMinimum());

        List<PointAttribute> pointAttributes = pointAttributeMapper.selectPointAttributeBysPointId(pointEquipment.getPointId());
        if (pointAttributes.size() == 0) {
            pointAttributeMapper.insertPointAttribute(pointAttribute);
        }else {
            pointAttributeMapper.reoupdatePointAttribute(pointAttribute);
        }

        List<PointEquipment> equipment = pointAttributeMapper.selectPointEquipmentByPointId(pointEquipment.getPointId());
        if(equipment.size() == 0) {
            pointEquipmentMapper.insertPointEquipment(pointEquipment);
        }else {
            pointEquipmentMapper.reoupdatePointEquipment(pointEquipment);
        }
        return 1;
    }

    /**
     * 修改点位与设备关联
     *
     * @param pointEquipment 点位与设备关联
     * @return 结果
     */
    @Override
    public int updatePointEquipment(PointEquipment pointEquipment)
    {
        return pointEquipmentMapper.updatePointEquipment(pointEquipment);
    }

    /**
     * 批量删除点位与设备关联
     *
     * @param ids 需要删除的点位与设备关联主键
     * @return 结果
     */
    @Override
    public int deletePointEquipmentByIds(Long[] ids)
    {
        return pointEquipmentMapper.deletePointEquipmentByIds(ids);
    }

    /**
     * 删除点位与设备关联信息
     *
     * @param id 点位与设备关联主键
     * @return 结果
     */
    @Override
    public int deletePointEquipmentById(Long id)
    {
        return pointEquipmentMapper.deletePointEquipmentById(id);
    }

    @Override
    public int deletePointEquipment(PointEquipment pointEquipment) {
        return pointEquipmentMapper.detetePointEquipmentByPointEquipment(pointEquipment);
    }
}
